package com.xy.shop.web.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xy.shop.common.dtgrid.model.Pager;
import com.xy.shop.common.utils.ExportUtils;
import com.xy.shop.web.service.ApplyOrderService;
import com.xy.shop.web.service.ImportOrderService;
import com.xy.shop.web.service.PayService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/shop/")
@Controller
public class ShopController extends BaseController{
    @Resource
    private ApplyOrderService applyOrderService;

    @Resource
    private PayService payService;

    @Resource
    private  ImportOrderService importOrderService;

    /**
     * @Description 获取订单点单
     * @Author zyy
     **/
    @GetMapping("getOrderUi")
    public String getOrderUi() {
        return "web/shop_order_list";
    }
    /**
     * @Description 茶馆创房统计
     * @Author zyy
     **/
    @ResponseBody
    @RequestMapping(value = "/getOrderList" , method = RequestMethod.POST)
    public Map<String, Object>  getOrderList(String gridPager, HttpServletResponse response)  {
            List<Map<String, Object>> list ;
        try {
            Pager pager = JSON.parseObject(gridPager, Pager.class);
            // 判断是否包含自定义参数
            Map<String, Object> parameters = pager.getParameters();
            //getAppParams(parameters);
            Page<Object> page = PageHelper.startPage(pager.getNowPage(),pager.getPageSize());
            // 3、判断是否是导出操作
            if (pager.getIsExport()) {
                if (pager.getExportAllData()) {
                    // 3.1、导出全部数据
                     list = applyOrderService.selectOrderList(parameters);
                    if (list== null) {
                        list  =new ArrayList<>();
                    }
                    ExportUtils.exportAll(response, pager, list);
                    return null;
                } else {
                    // 3.2、导出当前页数据

                        ExportUtils.export(response, pager);

                }
            } else {
                // 设置分页，page里面包含了分页信息
                list = applyOrderService.selectOrderList(parameters);
                if (list== null || list.size()==0) {
                    list  =new ArrayList<>();
                }
                return getReturnPage(pager, page, list);
            }
            } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 订单操作（支付，审核）
     * @param request
     * @param orderId
     * @param type
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/applyOrder" , method = RequestMethod.POST)
    public Map<String, Object> applyOrder(HttpServletRequest request , String orderId,String type) {
        Map<String, Object> result=payService.updateWxPayment(orderId,type);
        return result;
    }
    /**
    *@Description: 订单导入
    *@Param:  null
    *@Author: zyy
    *@date: 2020/6/22
    *@Time: 15:24
    */
    @RequestMapping(value = "0nekeyImportOrder" , method = RequestMethod.POST)
    @ResponseBody
    public Object list(@RequestParam(value="file",required = false)MultipartFile file, HttpServletRequest request,
                       HttpServletResponse response) {
        return  importOrderService.onekeyImportOrder(file);
    }
}
